<?php

namespace app\shopapi\model;



use app\api\model\ExtendConsume;
use app\api\model\UserConsumeLog;
use app\common\model\User;
use think\Db;
use think\Model;


class ExtendConsumeShop extends Model
{
    // 表名
    protected $name = 'extend_consume_shop';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;


    public static function addLog($consume_log_id,$shop_id,$commission)
    {
        $extend_consume_model = new ExtendConsumeShop();

        $data = [
            'consume_log_id'=>$consume_log_id,
            'shop_id'=>$shop_id,
            'commission'=>$commission
        ];

        $res = $extend_consume_model->save($data);

        return $res;
    }

    //发放佣金
    public static function sendCommission($consume_log_id)
    {
        $consume_info = UserConsumeLog::get($consume_log_id);
        $list = ExtendConsumeShop::where(['consume_log_id'=>$consume_log_id,'is_send'=>0])->select();
        foreach ($list as $k=>$v){
            $detail = ExtendConsumeShop::get($v['id']);
            $detail->is_send = 1;
            $detail->send_time = time();
            $detail->save();
            Shop::money($v['commission'],$v['shop_id'],'收益已到账,交易编号:'.$consume_info->order_no,1);
        }
        return true;
    }
}